package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private ReportMapper reportMapper;

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getTodayBusinessData() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        //查询当前营业额
        Double turnover = reportMapper.queryTurnover(begin, end);
        turnover = turnover == null? 0.0: turnover;
        //查询当天有效订单数
        Integer validOrderCount = reportMapper.queryValidOrder(begin, end);
        //计算平均客单价(营业额/有效订单数)
        Double unitPrice = validOrderCount == 0 ? 0.0 : (turnover.doubleValue() / validOrderCount);
        //查询当天所有订单数,用来计算订单完成率
        Integer allOrderCount = reportMapper.queryAllOrder(begin, end);
        //计算订单完成率
        Double orderCompleteRate = allOrderCount == 0 ?0.0 : (validOrderCount.doubleValue() / allOrderCount);
        //查询当前新增用户数
        Integer newUserCount = reportMapper.queryNewUser(begin, end);

        return new BusinessDataVO(turnover,validOrderCount,orderCompleteRate,unitPrice,newUserCount);
    }

    /**
     * 查看订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        Integer waitingOrders = reportMapper.findOrdersCountByStatus(Orders.TO_BE_CONFIRMED,begin,end);
        Integer deliveredOrders = reportMapper.findOrdersCountByStatus(Orders.CONFIRMED, begin, end);
        Integer completeOrders = reportMapper.findOrdersCountByStatus(Orders.COMPLETED, begin, end);
        Integer cancelledOrders = reportMapper.findOrdersCountByStatus(Orders.CANCELLED, begin, end);
        Integer allOrders = reportMapper.queryAllOrder(begin, end);
        return new OrderOverViewVO(waitingOrders,deliveredOrders,completeOrders,cancelledOrders,allOrders);
    }

    @Autowired
    private DishMapper dishMapper;
    /**
     * 菜品总览
     * @return
     */
    @Override
    public DishOverViewVO dishOverview() {
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        return new DishOverViewVO(sold,discontinued);
    }

    @Autowired
    private SetMealMapper setMealMapper;
    /**
     * 套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO setmealOverview() {
        Integer sold = setMealMapper.countByStatus(StatusConstant.ENABLE);
        Integer discontinued = setMealMapper.countByStatus(StatusConstant.DISABLE);
        return new SetmealOverViewVO(sold,discontinued);
    }
}
